<%@ page import="java.sql.Date" %>
<%@ page import="Unit2.controller.TestControllerForWeb" %>
<%@ page import="Unit2.controller.Authentification" %>
<%@ page import="Unit2.question.User" %>
<%@ page import="Unit2.question.UserList" %>
<%
    String message = null;

    // analyze form parameters
    int errorCount = 0;
    String login = request.getParameter("login");
    String password = request.getParameter("password");
    if (login == null || login.length() == 0) {
        message = "Login name is required";
        errorCount++;
    }

    // if no errors found, try authenticate user, otherwise display the same page
    if (errorCount == 0) {
        // prepare controller
        Authentification auth = new Authentification();

        // authenticate user
        if (!auth.checkLogin(login)) {
            // user succesully authenitcated, starting test
            //testController.ask();
            User tempUser = new User(login,password);
            UserList userList=(UserList)auth.load();
            userList.getUserList().add(tempUser);
            auth.save(userList);
            auth.createResultFile("C:\\Test_files/history/" + login + ".xml");
            response.sendRedirect("index.jsp");
            return;
        }
        else {
            message = "Invalid login name or password";
            errorCount++;
        }
    }
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <%@ page contentType="text/html;charset=UTF-8" language="java" %>

<title>Registration</title>


<link href="css/my.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body {
	background-image: url(images/paper_fibers.png);
	background-repeat: repeat;
}
</style>
</head>

<body>
<div style="width: 50%; margin: 0 auto; text-align: center;">

  <div id="login-form"  >

        <h3>Registration</h3>

        <fieldset>

            <form  method="post">
                <%=(message != null ? message : "")%>

                <input type="name" name ="login" required value="Login" onBlur="if(this.value=='')this.value='Login'" onFocus="if(this.value=='Login')this.value='' ">

                <input type="password" name ="password" required value="Password" onBlur="if(this.value=='')this.value='Password'" onFocus="if(this.value=='Password')this.value='' ">
                <input type="submit" value="Регистрация">

   

            </form>

        </fieldset>

    </div> 

 </div>
 
</body>
</html>
